package yl.hs.bmipfull.controllers.system;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.service.system.ISearchViewService;
import yl.hs.bmipfull.service.system.ISiteMessageMainService;
import yl.hs.bmipfull.utils.HsApp;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

/**
 * 作用:  站内信息控制层<br/>
 * 创建时间: 2021年05月12日 16时09分41秒 <br/>
 *
 * @author 张凯
 */
@Api(value = "站内信息Web接口", tags = "系统管理——站内信息操作")
@RequestMapping(value = "/api/message/send")
@RestController
public class SiteMessageMainController extends ControllerBase {

    @Autowired
    ISiteMessageMainService service;
    @Autowired
    ISearchViewService svSVC;

    @ApiOperation(value = "查询站内信息", notes = "查询站内信息")
    @RequestMapping(value = "/{rowNum}/{page}", method = RequestMethod.POST)
    public WebResult queryList(@RequestBody PageQuery query, @PathVariable int page, @PathVariable int rowNum) {
        try {
            var result = service.queryList(query, rowNum, page);
            svSVC.updateUserMenuView(query);
            return success("ok", result);
        } catch (Exception ee) {
            return error(ee);
        }
    }

    @ApiOperation(value = "查询站内信息收件信息", notes = "分页查询站内信息收件信息")
    @RequestMapping(value = "/queryMsgTypeCount", method = RequestMethod.GET)
    public WebResult queryMsgTypeCount() {
        try {
            var user= HsApp.getCurrent();
            var result = service.getTypeCount(user.getUserSN());
            return success("ok", result);
        } catch (Exception ee) {
            return error(ee);
        }
    }
    @ApiOperation(value = "查询站内信息", notes = "根据站内信息编号查询站内信息")
    @RequestMapping(value = "/{sn}", method = RequestMethod.GET)
    public WebResult findBySn(@PathVariable String sn) {
        return success("查询成功", service.findBySn(sn));
    }


    @ApiOperation(value = "保存站内信息", notes = "查询/更新站内信息信息")
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public WebResult saveMenu(@ApiParam(value = "提交信息") SubmitModel submitModel,
                              @ApiParam(value = "接收用户编号", allowMultiple = true, required = true) @RequestParam("userSn") String[] userSn,
                              @ApiParam(value = "收件类型", required = true) @RequestParam("receiptType") String[] receiptType) {
        try {
            Integer result = service.saveSiteMessageMain(submitModel.getData(), userSn, receiptType);
            if (result > 0) {
                return success("保存成功");
            } else {
                return fail("数据未保存");
            }
        } catch (Exception e) {
            return error(e);
        }

    }

    @ApiOperation(value = "删除站内信息信息", notes = "根据站内信息编号删除系站内信息信息")
    @RequestMapping(path = "", method = RequestMethod.DELETE)
    public WebResult delete(@ApiParam(value = "站内信息编号", name = "sn", allowMultiple = true, required = true) @RequestParam("sn") String[] sn) {
        if (service.delete(sn) > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }

    }
    @ApiOperation(value = "更改信件状态", notes = "更改信件状态")
    @RequestMapping(path = {"/updatestate/{sn}","/updatestate"}, method = RequestMethod.GET)
    public WebResult updateState(@ApiParam(value = "sn",name = "信件编号",required = false) @PathVariable(required = false) String sn){
        try {
            var result = service.updateState(sn,(short) 1);
            if(result>0){
                return success("操作成功");
            }
            return fail("操作失败");
        }catch (Exception ee){
            return  error(ee);
        }

    }
}